package com.fancy.learncenter.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;

import com.andview.refreshview.XRefreshView;
import com.fancy.learncenter.activity.base.BaseActivity;
import com.fancy.learncenter.adapter.ShouDaoAdapter;
import com.fancy.learncenter.bean.BaseDataBean;
import com.fancy.learncenter.bean.ReciveResumeDataBean;
import com.fancy.learncenter.bean.TzDataBean;
import com.fancy.learncenter.common.Constant;
import com.fancy.learncenter.net.HttpMehtod;
import com.fancy.learncenter.net.IdeaObserver;
import com.fancy.learncenter.utils.LogUtil;
import com.fancy.learncenter.view.CustomFootView;
import com.fancy.learncenter.view.CustomHeaderView;
import com.superservice.lya.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;

public class GetResumeActivity extends BaseActivity {

    @Bind(R.id.recycleView)
    RecyclerView recycleView;
    @Bind(R.id.prompt_img)
    ImageView promptImg;
    @Bind(R.id.xRefreshView)
    XRefreshView xRefreshView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fancy_move, getResources().getColor(R.color.toolbar_color));
        ButterKnife.bind(this);
        initToolbar("收到的简历");

        adapter = new ShouDaoAdapter(this, new ArrayList<ReciveResumeDataBean>());
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recycleView.setLayoutManager(linearLayoutManager);
        recycleView.setAdapter(adapter);

        adapter.setCustomLoadMoreView(new CustomFootView(this));
        xRefreshView.setPullLoadEnable(true);
        xRefreshView.setCustomHeaderView(new CustomHeaderView(this));
        xRefreshView.setPinnedTime(1000);
        xRefreshView.setXRefreshViewListener(new XRefreshView.XRefreshViewListener() {
            @Override
            public void onRefresh() {
                LogUtil.MyLog("onLoadMore", "====onRefresh==");
                recieveResume(true);
            }

            @Override
            public void onRefresh(boolean isPullDown) {

            }

            @Override
            public void onLoadMore(boolean isSilence) {
                recieveResume(false);
                LogUtil.MyLog("onLoadMore", "====onLoadMore==");
            }

            @Override
            public void onRelease(float direction) {

            }

            @Override
            public void onHeaderMove(double headerMovePercent, int offsetY) {

            }
        });


        recieveResume(true);
    }

    ShouDaoAdapter adapter;
    int page = 0;

    public void recieveResume(final boolean isRefresh) {
        Map map = new HashMap();


        if (isRefresh) {
            page = 0;
        } else {
            page++;
        }
        map.put("page", page + "");


        HttpMehtod.getInstance().recieveResume(map, new IdeaObserver<BaseDataBean<List<ReciveResumeDataBean>>>(this, true) {

            @Override
            public void onSuccess(BaseDataBean<List<ReciveResumeDataBean>> listBaseDataBean) {

//                if (listBaseDataBean.getData() != null) {
//                    promptImg.setVisibility(View.GONE);
//                    adapter.notifyDataSetChanged((ArrayList<ReciveResumeDataBean>) listBaseDataBean.getData());
//                } else {
//                    promptImg.setVisibility(View.VISIBLE);
//                }


                if (isRefresh) {

                    if (xRefreshView == null) {
                        return;
                    }
                    xRefreshView.stopRefresh();
                    xRefreshView.setLoadComplete(false);

                    if (listBaseDataBean.getData() == null || listBaseDataBean.getData().size() <= 0) {
                        promptImg.setVisibility(View.VISIBLE);
                        adapter.notifyDataSetChanged(new ArrayList<ReciveResumeDataBean>());
                        return;
                    } else {
                        promptImg.setVisibility(View.GONE);

                        adapter.notifyDataSetChanged((ArrayList<ReciveResumeDataBean>) listBaseDataBean.getData());
                    }

                } else {
                    if (listBaseDataBean.getData() == null) {
                        xRefreshView.setLoadComplete(true);
                    } else if (listBaseDataBean.getData() != null && listBaseDataBean.getData().size() < Constant.PAGE_NUM) {
                        xRefreshView.setLoadComplete(true);
                    } else {
                        xRefreshView.stopLoadMore();
                    }


                    if (listBaseDataBean.getData() == null) {
                        return;
                    }
                    adapter.addAll(listBaseDataBean.getData());
                }


            }

            @Override
            public void onFail(int errorCode) {
                xRefreshView.stopRefresh();
                xRefreshView.setLoadComplete(false);
            }

        });
    }
}
